<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<link rel="stylesheet" href="${ctx}/css/common.css" />
<link rel="stylesheet" href="${ctx}/css/validate.css" />
<link rel="stylesheet" href="${ctx}/css/dialog/skin/bluebar/ymPrompt.css" />

<script type="text/javascript" src="${ctx}/js/jquery.js"></script>
<script type="text/javascript" src="${ctx}/js/dialog/ymPrompt.js"></script>

<script language="javascript">
<c:if test="${result == 'Success'}">
ymPrompt.succeedInfo("<s:text name='AssignmentAuthsSuccess' />！",null,null,null,closeWin);
</c:if>

$(document).ready(function() {
	var ajaxUrl = "../ajax/getAuths.action?accountid=${account.id }";
	$.getJSON(ajaxUrl,function(data){
		$.each(data,function(id,value){
			$("[type='checkbox']").each(function(){
				if($(this).val() == value.id){
					$(this).attr("checked",true);
				}
			});
		});
	});
});

function selectMenu(obj){
	if($(obj).attr("checked")){ 
		$("[parent='"+obj.id+"']").each(function(){
			$(this).attr("checked",true);
			changeButtons(this);
		})
	} else {
		$("[parent='"+obj.id+"']").each(function(){
			$(this).attr("checked",false);
			changeButtons(this);
		})
	}
}

function changeButtons(obj){
	if($(obj).attr("checked")){
		$("[parent='"+obj.id+"']").each(function(){
			$(this).attr("checked",true);
		})
	} else {
		$("[parent='"+obj.id+"']").each(function(){
			$(this).attr("checked",false);
		})
	}
}

function selectPage(obj){
	var menu = $("#" + $(obj).attr("parent"));
	
	if($(obj).attr("checked") && !menu.attr("checked")){
		menu.attr("checked",true); //选择菜单
		changeButtons(obj); //选择按钮
	} else {
		//取消按钮选择
		changeButtons(obj); //选择按钮
		
		//判断是否取消菜单选择
		var i = 0;
		$("[parent='"+$(obj).attr("parent")+"']").each(function(){
			if($(this).attr("checked")){
				i++;
			}
		});
		
		if(i == 0){
			menu.attr("checked",false);
		}
	}
}

function selectButton(obj){
	var page = $("#" + $(obj).attr("parent"));
	
	if($(obj).attr("checked") && !page.attr("checked")){
		page.attr("checked",true); //选择页面
		var menu = $("#" + $(page).attr("parent"));
		menu.attr("checked",true); //选择菜单
	}
}

function formSubmit(){
	var selectCount = 0;
	
	$("[type='checkbox']").each(function(){
		if($(this).attr("checked")){
			selectCount++;
		}
	});
	
	if(selectCount == 0){
		$("#errMsg").text("<s:text name='SelectEntityIsNull' />");
		return false;
	} else {
		$("#submitForm").submit();
	}
}

//关闭对话框
function closeWin(){
	parent.ymPrompt.close();
}
</script>
</head>

<body>
	<div style="width:100%;height:30px;line-height: 30px;">
		<s:text name="AccountName" />：${account.name }
		<span id="errMsg" class="inputMsg"></span>
	</div>
	<form id="submitForm" action="assignmentAuths.action" method="post">
		<input id="accountId" name="accountId" type="hidden" value="${account.id }" />
		
		<c:forEach var="menu" items="${entitys}">
			<div class="table_header">
				<input id="menu_${menu.id }" name="entityIds" type="checkbox" value="${menu.id }" onchange="selectMenu(this)"/><span>${menu.entityName }</span>
			</div>
			<div class="table_content">
				<c:forEach var="page" items="${menu.child}">
					<input id="page_${page.id }" name="entityIds" parent="menu_${menu.id }" type="checkbox" value="${page.id }"  onchange="selectPage(this)" />${page.entityName }
						<c:forEach var="button" items="${page.child}">
						<input id="button_${button.id }" name="entityIds" parent="page_${page.id }" type="checkbox" value="${button.id }"  onchange="selectButton(this)" />${button.entityName }
						</c:forEach>
					<br />
					<div class="clear"></div>
				</c:forEach>
			</div>
			<div class="clear"></div>
		</c:forEach>
		
		<div style="width:100%;height:30px;line-height: 30px;text-align:center;">
		<input id="submitBtn" name="submitBtn" type="button" onclick="formSubmit();" value="<s:text name='Save' />" />
		<input id="submitBtn" name="submitBtn" type="button" onclick="closeWin();" value="<s:text name='Close' />" />
		</div>
	</form>
</body>

</html>
